<?php
declare (strict_types=1);

namespace app\business\controller;

use app\common\model\Assets;
use app\common\model\BusinessVideo;
use app\common\model\User;
use app\common\model\UserVideoComment;
use app\common\model\UserVideoComment as Comment;
use app\common\model\Business;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;

class VideoComment extends Base
{
    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\UserVideoComment';

    //关联模型
    protected $hasOne = ['video', 'user'];

    protected $append = ['video_link_url'];

    //额外查询条件
    protected $extraSearch = ['business_id'];

    public function index()
    {
        $extra_map = ['business_id'=>$this->admin['business_id']];
        return $this->setTable($extra_map);
    }

    //自动构建table,按需修改
    public function setTable($extra_map=[])
    {
        //构建table
        $tableFront = TableFront::getInstance()
            ->addRow(['fixed' => 'left', 'type' => 'checkbox', 'title' => '全选'])
            //->addRow(['field' => 'id', 'title' => 'ID', 'sort' => true, 'width' => 80])
            ->addRow(['field' => 'icon', 'title' => '视频', 'width' => 90, 'templet' => '#iconTpl', 'fixed' => 'left'])
            ->addRow(['field' => 'introduce', 'title' => '视频介绍', 'width' => 300, 'templet' => '<div>{{d.video?d.video.introduce:""}}</div>', 'fixed' => 'left'])
            ->addRow(['field' => 'user', 'title' => '会员', 'templet' => '<div>{{d.user?d.user.nickname:""}}</div>'])
            ->addRow(['field' => 'comment', 'title' => '评论内容', 'width' => 800])
            ->addRow(['field' => 'create_time', 'title' => '评论时间'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 100])
            ->clearTable();

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addTopButtons(['deleteAll'])
            ->addRightButtons(['delete'])
            ->setSearch($this->setSearch(), false)
            ->addExtraMap($extra_map)
            ->addExtraHtml(['VideoComment'])
            ->fetch();
    }

    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['video_id', '视频标题', 'input', '', '\app\common\model\BusinessVideo', 'title,id'])
            ->setSearch(['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id']);
    }

    public function delete($key = 'is_del'): Json
    {
        $ids = $this->request->param('id');
        if(is_array($ids)) $ids = implode(',', $ids);
        $result = UserVideoComment::del($ids, $this->admin['business_id']);

        return json($result);
    }

    public function pay()
    {
        $param = $this->request->param();

        if($this->request->isPost()){
            $user = User::with(['assets'])->find($param['uid']);
            $result = UserVideoComment::payment($param, $user);
            return json($result);
        }
        View::assign([
            'amount' => $param['amount'],
            'business_id' => $param['business_id'],
            'uid' => Business::where('id', $param['business_id'])->value('uid'),
            'ids' => $param['ids']
        ]);
        return View::fetch('video_comment/pay');
    }

    public function balance()
    {
        $param = $this->request->param();

        $assets = Assets::where('uid', $param['id'])->value($param['val']);
        if ($assets) {
            return json(['code' => 1, 'msg' => '', 'result' => $assets]);
        } else {
            return json(['code' => -1, 'msg' => '加载失败']);
        }
    }
}